class Car{
    constructor(options){
        this.cont = options.cont;
        this.url = options.url;
        this.str = ""
        this.zongjia = 0
        this.getCookie()
        
        
        this.addEvent()
    }
    load(){
        // console.log(this.dataName,this._id)
        var that = this;
        $.ajax({
            url:this.url,
            type:'get',
            data:{
                dataName:this.dataName,
                _id:this._id,
                num:this.num
            },
            success:function(res){
                that.res = res;
                // console.log(res)
                that.display()
            }
        })
    }
    getCookie(){
        this.goods = JSON.parse($.cookie("goods"));
        // console.log(this.goods)
        for(var i = 0;i<this.goods.length;i++){
            this.dataName=this.goods[i].dataName
            this.num = this.goods[i].num
            // console.log(this.num)
            this._id = this.goods[i]._id

            this.load()
        }
        // 渲染页面
        
    }
    display(){
        // var str = "";
        // var zongjia = 0;
        // 获取数据
        // for(var i=0;i<this.res.length;i++){
        // 	for(var k=0;k<this.res[i].length;k++){
        //     for(var j=0;j<this.goods.length;j++){
        //         if(this.goods[j].id === this.res[i][k].id){
        //             str += `<tr data-id="${this.res[i][k].id}">
        //                         <td><img src="${this.res[i][k].img}"/></td>
        //                         <td>${this.res[i][k].info}</td>
        //                         <td class="price">${this.res[i][k].price}</td>
        //                         <td><input type="number" id="num" value="${this.goods[j].num}" min=1></td>
        //                         <td class="totalprice">${this.res[i][k].price*this.goods[j].num}</td>
        //                         <td><em>删除</em></td>
        //                     </tr>`
        //             zongjia += this.res[i][k].price*this.goods[j].num
        //         }
        //      }
        //    }
        // }
        this.str += 
        `<tr data-id="${this.res.page_data._id}">
                                <td><img src="${this.res.page_data.detail.auth_icon}"/></td>
                                <td>${this.res.page_data.title}</td>
                                <td class="price">${this.res.page_data.des}</td>
                                <td><input type="number" id="num" value="${this.res.num}" min=1></td>
                                <td class="totalprice">${this.res.page_data.des*this.res.num}</td>
                                <td><em>删除</em></td>
                             </tr>`
                    this.zongjia += this.res.page_data.des*this.res.num
                             
        this.cont.html(this.str);
        $(".zongjia").html(this.zongjia)
    
        
    }
    
    addEvent(){
        var that = this;
       
        this.cont.on("click","em",function(){
        	var sum =0
            that._id = $(this).parent().parent().attr("data-id");
            $(this).parent().parent().remove();
            that.setCookie(function(index){
                that.goods.splice(index,1)
                
                 var money = $("tbody tr").children(".totalprice")
//          console.log(money[0].innerHTML)
            
            
            for(var i=0;i<money.length;i++){
            	sum +=parseFloat($(money[i]).html())
            }
            
             $(".zongjia").html(sum)
            })
        })
        this.cont.on("input","#num",function(){
        	//声明总价 每次数字改变就重新计算
        	 var sum = 0;
            that._id = $(this).parent().parent().attr("data-id");
            // console.log(that._id)
            that.num = $(this).val();
            // console.log(that.num)
            that.setCookie(function(index){
                // console.log(index)
                that.goods[index].num = parseInt(that.num);
            })
            
            that.price = parseInt($(this).parent().siblings(".price").html())
            
             $(this).parent().siblings(".totalprice").html(parseFloat(that.price*that.num))
             
             
            var money = $("tbody tr").children(".totalprice")
//          console.log(money[0].innerHTML)
            
            
            for(var i=0;i<money.length;i++){
            	sum +=parseFloat($(money[i]).html())
            }
            
             $(".zongjia").html(sum)
        })
        
        
    }
    setCookie(cb){
        // 7.修改cookie，可以删，可以改，取决于回调函数中怎么做
        for(var i=0;i<this.goods.length;i++){
            // console.log(this.goods[i]._id)
            if(this.goods[i]._id == this._id){
                
                cb(i)
            }
        }
        // 8.不管如何都要将cookie设置回去，因为上面所有操作，都只是在操作数组
        $.cookie("goods",JSON.stringify(this.goods))
        // console.log(this.goods)
        
        // this.goods = JSON.parse($.cookie("goods"));
    }
}


new Car({
    cont:$("tbody"),
    url:"api/detail",
    
})